<?php

namespace app\index\controller\v1\wechat;


use app\common\controller\WechatController;
use app\common\model\wechat\WechatCoupon;
use hiduoke\services\facade\WechatService as Ws;
use think\App;

class CheckCoupon extends WechatController
{
    /**
     * @var WechatCoupon
     */
    public $model;
    /**
     * @var mixed|string
     */
    private $user;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->user = $this->getUser();
        $this->model = new WechatCoupon();
    }

    public function index()
    {
        $this->assign('title', "卡券核销");
        return $this->fetch("index");
    }

    /**
     * 获取卡券列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCouponList()
    {
        $res = $this->model->getCheckCouponUserList($this->user['id']);
        return $this->success($res['count'], $res['list']);
    }

    public function getCoupon()
    {
        $key = input('key');
        $coupon = $this->model->getCheckCoupon(['key' => $key]);
        $check = $this->model->checkCouponStatus($coupon);
        if ($check['code'] == -1) {
            return $this->error($check['msg']);
        }
        return $this->success('ok', $coupon);
    }

    /**
     * 卡券核销
     */
    public function check()
    {
        $key = input('key');
        return $this->model->checkCoupon($key);
    }

}